function Tank(context,imgKr,imgBr,startX,startY,startCorpusWidth,startTurretWidth,startSpeedT,startSpeedK,startSpeedB,startHP,startDamage,startRate,control,name){
    Tank.superclass.constructor.call(this);
    var _instance=this;
    
    /* Cords */
    _instance.x;
    _instance.y;
    /* pick cords */
    var pickX;
    var pickY;
    var pickDegreesT;

	        /* lable */
    var canB = document.createElement('canvas');
    var ctxB = canB.getContext('2d');
    var canT = document.createElement('canvas');
    var ctxT = canT.getContext('2d');
    var canBu = document.createElement('canvas');
    var ctxBu = canBu.getContext('2d');
	
	
    /* tank */
    /* private */
    var speedT;
    var speedB;
    var speedK;
    var maxSpeedT;
    var maxSpeedK;
    var maxSpeedB;
    var rate;
    /* publick */
    _instance.corpusWidth;
    _instance.corpusHeight;
    _instance.turretWidth;
    _instance.turretHeight;
    _instance.HP;
    _instance.maxHP;
    _instance.damage;
    _instance.degreesB;
    _instance.degreesT;
    _instance.nick;
    
    /* constructor */
    (function(){
        _instance.x = startX;
        _instance.y = startY;
        _instance.damage = startDamage;
        _instance.HP = startHP;
        _instance.degreesB = 0;
        _instance.degreesT = 0;
        _instance.maxHP = startHP;
        _instance.corpusWidth  = startCorpusWidth;
        _instance.corpusHeight = _instance.corpusWidth/1.5;
        _instance.turretWidth = startTurretWidth;
        _instance.turretHeight = _instance.turretWidth/3;
        _instance.nick = name;
        speedT = 0;
        speedB = 0;
        speedK = 0;
        maxSpeedT = startSpeedT;
        maxSpeedK = startSpeedK;
        maxSpeedB = startSpeedB;
        rate = startRate;
    })();
    
    /* public  */
    _instance.Replace = function(){
        
                 /*keyboard mode*/
                if (_instance.x+speedT*(Math.cos(_instance.degreesT.degree()))>_instance.corpusWidth/2 && _instance.x+speedT*(Math.cos(_instance.degreesT.degree()))<600-_instance.corpusWidth/2)
                {
                    _instance.x+=speedT * (Math.cos(_instance.degreesT.degree()));
                }
                if (_instance.y+speedT*(Math.sin(_instance.degreesT.degree()))>_instance.corpusWidth/2+30 && _instance.y+speedT*(Math.sin(_instance.degreesT.degree()))<600-_instance.corpusWidth/2)
                {    
                 _instance.y+=speedT * (Math.sin(_instance.degreesT.degree()));
                }
                _instance.degreesT += speedK;
                _instance.degreesB = _instance.degreesB+speedB + speedK;
                
                /*mouse mode*/
                if (pickX!==undefined)
                    {
                        /*rotate tank (mouse)*/
                        if (Math.abs(pickDegreesT-_instance.degreesT)>maxSpeedK){
                            if (pickDegreesT>_instance.degreesT&&pickDegreesT-_instance.degreesT<180)
                                {
                                    speedK=maxSpeedK;
                                }
                            if (pickDegreesT>_instance.degreesT&&pickDegreesT-_instance.degreesT>180)
                                {
                                    speedK=-maxSpeedK;
                                }    
                            if (pickDegreesT<_instance.degreesT&&_instance.degreesT-pickDegreesT<180)
                                {
                                    speedK=-maxSpeedK;
                                }
                            if (pickDegreesT<_instance.degreesT&&_instance.degreesT-pickDegreesT>180)
                                {
                                    speedK=maxSpeedK;
                                }   
                            pickDegreesT =  Math.atan2(pickY-_instance.y,pickX-_instance.x);
                            if (pickDegreesT<0) {pickDegreesT += 2*Math.PI;}
                            pickDegreesT = pickDegreesT*180/Math.PI;    
                       } else {
                           _instance.degreesB+=pickDegreesT-_instance.degreesT;
                           _instance.degreesT=pickDegreesT;
                           speedK=0;
                       }
                       
                       /*move tank (mouse)*/
                       if (Math.abs(_instance.x-pickX)>maxSpeedT||Math.abs(_instance.y-pickY)>maxSpeedT)
                           {
                               speedT=maxSpeedT;
                           } else {
                               _instance.x=pickX;
                               _instance.y=pickY;
                               speedT=0;
                           }
                          
                    }
                if (_instance.degreesT>=360) {_instance.degreesT-=359;}
                if (_instance.degreesT<0) {_instance.degreesT+=359;}
                if (_instance.degreesB>=360) {_instance.degreesB-=359;}      
				if (_instance.degreesB<0) {_instance.degreesB+=359;}    
    };   
     
	 _instance.Draw = function() {
	      /* drow corpus */
                var sizeT = _instance.corpusWidth;
        
                canT.width = sizeT*2  ;
                canT.height = sizeT*2  ;
                ctxT.translate(sizeT, sizeT);
                ctxT.rotate(_instance.degreesT.degree());
                ctxT.drawImage(imgKr, -_instance.corpusWidth/2, -_instance.corpusHeight/2,_instance.corpusWidth,_instance.corpusHeight);
                context.drawImage(canT, _instance.x-sizeT, _instance.y-sizeT);
                context.font = "italic 10pt Arial";
                context.fillStyle = "#000";
                context.fillText(_instance.nick,_instance.x-_instance.nick.length,_instance.y-sizeT/2*1.5);
             
          /* drow turret */
       
                var sizeB = _instance.corpusWidth;
        
                canB.width = sizeB*2  ;
                canB.height = sizeB*2  ;
                ctxB.translate(sizeB, sizeB);
                ctxB.rotate(_instance.degreesB.degree());
                ctxB.drawImage(imgBr, -_instance.turretWidth/2+10, -_instance.turretHeight/2,_instance.turretWidth,_instance.turretHeight);
                context.drawImage(canB, _instance.x-sizeB, _instance.y-sizeB);
     }
	 
     /* rad => degree */
     Number.prototype.degree = function () {
        return this * Math.PI / 180;
     };
   
   if (control === 2) {
     /*  keyboard */
     document.body.onkeydown = function() {
        switch(event.keyCode)     
        {
            case 87: {speedT=maxSpeedT; break}
            case 83: {speedT=-maxSpeedT; break}
            case 68: {speedK=maxSpeedK; break}   
            case 65: {speedK=-maxSpeedK; break}
            case 37: {speedB=-maxSpeedB; break}
            case 39: {speedB=maxSpeedB; break}
            case 32: {a.createBullet(); break}       
        }
    };    
    document.body.onkeyup = function() {
        switch(event.keyCode)     
        {
            case 87: {speedT=0; break}
            case 83: {speedT=0; break}
            case 68: {speedK=0; break}   
            case 65: {speedK=0; break}
            case 37: {speedB=0; break}
            case 39: {speedB=0; break}
        }
    };
   } else {
          _instance.move = function(event){
               pickX = event.pageX-366;
               pickY = event.pageY-24;
               pickDegreesT =  Math.atan2(pickY-_instance.y,pickX-_instance.x);
               if (pickDegreesT<0) {pickDegreesT += 2*Math.PI;}
               pickDegreesT = pickDegreesT*180/Math.PI;
          };
          document.body.onkeydown = function() {
              switch(event.keyCode)     
              {
                  case 37: {speedB=-maxSpeedB; break}
                  case 39: {speedB=maxSpeedB; break}
                  case 32: {a.createBullet(); break}    
              }
          };    
          document.body.onkeyup = function() {
              switch(event.keyCode)     
              {
                  case 37: {speedB=0; break}
                  case 39: {speedB=0; break}
              }
    };
   }
};


window.extendClass(Tank, Object);
